package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.CommonResonseBean;
import com.tongyi.lizhishop.common.BaseActivity;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.managers.APIServiceManager;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class InviteFriendsActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.activity_invite_friends)
    LinearLayout activityInviteFriends;
    @BindView(R.id.qrcodeimage)
    ImageView qrcodeimage;
    @BindView(R.id.huodongguize)
    TextView huodongguize;
    private TextView rightTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_friends);
        ButterKnife.bind(this);
        initTitleBarView(titlebar, "邀请好友");
        initView();
        loadData();
    }

    private void loadData() {


        APIServiceManager.getInstance().create(MessageApi.class)
                .addFriend(SPUtils.getInstance().getString("userid"))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<CommonResonseBean>() {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        String image = value.getImage();
                        if (!TextUtils.isEmpty(image)) {
                            Glide.with(InviteFriendsActivity.this).
                                    load(APIServiceManager.server_address + image).into(qrcodeimage);
                        } else {
                            ToastUtils.showShort("请求参数有误!");
                        }
                        LogUtils.w(image);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        ToastUtils.showShort("无法链接网络");
                    }
                });
    }

    private void initView() {
        rightTextView = titlebar.getRightTextView();
        rightTextView.setText("分享");
        rightTextView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == rightTextView) {
            showShare();
        }
    }

    private void showShare() {
        OnekeyShare oks = new OnekeyShare();

        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // 分享时Notification的图标和文字  2.5.9以后的版本不     调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        //  oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");
        // 启动分享GUI
        oks.setCallback(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        oks.show(this);
    }

    @OnClick(R.id.huodongguize)
    public void onClick() {
        Bundle bundle = new Bundle();
        bundle.putString("titlebar", "活动规则");
        bundle.putString("url", "/frontend.php/Img/tuijianguize");
        ActivityUtils.startActivity(bundle, this, PlatformIntroduceActivity.class);
    }
}
